<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=
    , initial-scale=1.0"
    />
    <title>Document</title>
  </head>
  <body>
    <!-- 第一题冒泡升序 -->
    <!-- <script>
      var arr = [5, 1, 4, 2, 8];
      function bubbleSort(arr) {
        if (!Array.isArray(arr)) {
          return;
        }

        if (arr.length == 0) {
          return;
        }

        for (var i = 0; i < arr.length - 1; i++) {
          var isSort = true;
          for (var j = 0; j < arr.length - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
              [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
              var isSort = false;
            }
          }

          if (isSort) {
            return arr;
          }
        }
      }

      console.log(bubbleSort(arr));
    </script> -->

    <!-- 第二题插入排序 -->
    <!-- <script>
     function insertionSort(arr) {
     for (let i = 1; i < arr.length; i++) {
        let key = arr[i]; // 当前要插入的元素
        let j = i - 1;
        // 将大于 key 的元素向右移动
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
        }
        
        // 将 key 插入到正确的位置
        arr[j + 1] = key;
      }
     }

     let arr = [1, 3, 5, 7, 2, 4, 6];
     insertionSort(arr);
     console.log("排序后的数组:", arr);

    </script> -->

    <!-- 第三题快速排序升序 -->
    <!-- <script>
      var arr = [1, 3, 5, 1, 4, 2, 8];
      function quilySort(arr) {
        if (arr.length <= 1) {
          return arr;
        }
        var left = [];
        var right = [];
        var center = arr[Math.floor(arr.length / 2)];
        for (var i = 0; i < arr.length; i++) {
          if (arr[i] < center) left.push(arr[i]);
          if (arr[i] > center) right.push(arr[i]);
        }

        return quilySort(left).concat([center], quilySort(right));
      }

      console.log(quilySort(arr));
    </script> -->

    <!-- 第四题冒泡排序包含重复元素 -->
    <!-- <script>
      var arr = [1, 3, 5, 1, 4, 2, 8, 5];
      function moveAgain() {
        for (var i = 0; i < arr.length - 1; i++) {
          for (var j = 0; j < arr.length - 1 - i; j++) {
            if (arr[j] >= arr[j + 1]) {
              [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
            }
          }
        }
        return arr;
      }

      console.log(moveAgain(arr));
    </script> -->

    <!-- 第五题 -->
    <!-- <script>
      function insertionSort(arr) {
        for (let i = 1; i < arr.length; i++) {
          let key = arr[i];
          let j = i - 1;

          // 将比 key 大的元素移动到右边
          while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
          }

          // 插入 key 到正确的位置
          arr[j + 1] = key;
        }
      }

      let strings = ["apple", "banana", "cherry", "date", "elderberry"];
      insertionSort(strings);
      console.log(strings);
    </script> -->

    <!-- 第六题 -->
    <!-- <script>
      function quickSort(arr) {
        if (arr.length <= 1) {
          return arr;
        }

        const pivot = arr[Math.floor(arr.length / 2)];
        const left = [];
        const right = [];
        const equal = [];

        for (let i = 0; i < arr.length; i++) {
          if (arr[i] < pivot) {
            left.push(arr[i]);
          } else if (arr[i] > pivot) {
            right.push(arr[i]);
          } else {
            equal.push(arr[i]);
          }
        }

        return [...quickSort(left), ...equal, ...quickSort(right)];
      }

      function generateRandomArray(size, min, max) {
        return Array.from(
          { length: size },
          () => Math.floor(Math.random() * (max - min + 1)) + min
        );
      }

      // 生成随机数组
      const randomArray = generateRandomArray(20, 1, 100);
      console.log("随机生成的数组:", randomArray);

      // 对数组进行快速排序
      const sortedArray = quickSort(randomArray);
      console.log("排序后的数组:", sortedArray);
    </script> -->
  </body>
</html>
